package com.atguigu.gmall.realtime.util;

import com.atguigu.gmall.realtime.bean.TransientSink;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.connector.jdbc.JdbcStatementBuilder;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;

import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/23 9:37
 */
public class MySinkUtil {
    
    public static <T> SinkFunction<T> getClickHouseSink(String db,
                                                                   String table,
                                                                   Class<T> tClass) {
        
        String url = "jdbc:clickhouse://hadoop162:8123/" + db;
        String driver = "ru.yandex.clickhouse.ClickHouseDriver";
        
        // insert into visitor_stats_2021(c1, c2, c3,...) values(?,?,?...)
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(table).append("(");
        // TODO
        final Field[] fields = tClass.getDeclaredFields();
        for (Field field : fields) {
            // 如果属性被transient修饰, 则表示不会写入到ClickHouse中
            if (field.getAnnotation(TransientSink.class) == null) {
                final String fieldName = field.getName();
                sql.append(fieldName).append(",");
            }
        }
        // 去掉最后一个逗号
        sql.deleteCharAt(sql.length() - 1);
        sql.append(" ) values ( ");
        for (Field field : fields) {
            if (field.getAnnotation(TransientSink.class) == null) {
                sql.append("?,");
            }
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(" )");
        
        return getJDBCSink(url, driver, sql.toString());
    }
    
    public static <T> SinkFunction<T> getJDBCSink(String url,
                                                  String driver,
                                                  String sql) {
        // jdbc connector 只支持sink
        return JdbcSink.<T>sink(sql,
                                new JdbcStatementBuilder<T>() {
                                    @Override
                                    public void accept(PreparedStatement ps,
                                                       T t) throws SQLException {
                                        // 3种获取class:  A.class  Class.forName(字符串类名)  对象.getClass
                                        final Field[] fields = t.getClass().getDeclaredFields();
                                        try {
                                            for (int i = 0, position = 1; i < fields.length; i++) {
                                                if (fields[i].getAnnotation(TransientSink.class) == null) {
                                                    fields[i].setAccessible(true);
                                                    ps.setObject(position++, fields[i].get(t));
                                                }
                                            }
                                        } catch (IllegalAccessException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                },
                                new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                                    .withUrl(url)
                                    .withDriverName(driver)
                                    .build());
    }
    
}
